home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d3 / jlp10.arc / LETPROC.C next >
C/C++ Source or Header  |  1990-04-08  |  8KB  |  312 lines

  1. char copyright[]=
  2.     "Julia's Letter Processor 1.0, Copyright (c) 1990 Michael Deuel Sullivan";
  3. char address[]=
  4.     "6011 33rd Street, N.W., Washington, D.C.   20015";
  5. char license[]=
  6.     "License granted without charge for non-commercial and educational use.";
  7.  
  8. /*  Command line options:
  9. **  L - enable lower case letters (default caps only)
  10. **  S - enable symbols (default alphanumeric only)
  11. */
  12.  
  13. #include <graphics.h>
  14. #include <conio.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <time.h>
  18. #include <dos.h>
  19. #include <math.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <alloc.h>
  23. #include <string.h>
  24.  
  25. #define MIDDLE_C 256.
  26. #define LEN 200
  27. #define setport(x) setviewport(x.left,x.top,x.right,x.bottom,x.clip)
  28.  
  29. int scale[]=
  30.     {0,2,4,5,7,9,11,12,14,16,17,19,21,23,24,26,28,29,31,33,35,36,38,40,41,43};
  31. int song[]=
  32.     {0,0,7,7,9,9,7,5,5,4,4,2,2,2,2,0,7,7,5,4,4,2,7,5,4,2};
  33. int len[]=
  34.     {2,2,2,2,2,2,4,2,2,2,2,1,1,1,1,4,2,2,4,2,2,4,4,4,2,4};
  35. double freq[44];
  36.  
  37. char *end_song[]=
  38.     { "NOW ", "I ", "KNOW ", "MY ", "A-", "B-", "C'S",
  39.     "NEXT ", "TIME ", "WON'T ", "YOU ", "SING ", "", "WITH ", "", "ME " };
  40. char *lines[5]={
  41.     "JULIA'S LETTER PROCESSOR!",
  42.     "ABCDEFGHIJKLM",
  43.     "NOPQRSTUVWXYZ",
  44.     "NOW I KNOW MY A-B-C'S",
  45.     "NEXT TIME WON'T YOU SING WITH ME ?"};
  46. double stepmult;
  47. int max_x, max_y;
  48. char caps_only=1;
  49. char alnum_only=1;
  50.  
  51. void initialize();
  52. void getopts(int argc, char **argv);
  53. void copyright_license();
  54. void intro_song();
  55. void do_letters();
  56. void put_exit_msg();
  57. void twirp();
  58.  
  59. main (int argc, char **argv) {
  60.     initialize();
  61.     if (argc>1) getopts(argc,argv);
  62.     copyright_license();
  63.     intro_song();
  64.     cleardevice();
  65.     put_exit_msg();
  66.     do_letters();
  67.     closegraph();
  68. }/* main() */
  69.  
  70. void initialize () {
  71.     int   gdriver=DETECT, gmode, g_errorcode;
  72.     int   i;
  73.  
  74.     for (i=0;i<44;i++) /* sets frequencies for upper half of keyboard */
  75.         freq[i]=exp((double)i*log(2.)/12.)*MIDDLE_C;
  76.     randomize();
  77.  
  78.     if (registerbgidriver(EGAVGA_driver)<0) {
  79.         printf("Error registering EGAVGA\n"); exit(1);
  80.     }
  81.     if (registerbgifont(small_font)<0) {
  82.         printf("Error registering small_font\n"); exit(1);
  83.     }
  84.     if (registerbgifont(sansserif_font)<0) {
  85.         printf("Error registering sansserif_font\n"); exit (1);
  86.     }
  87.     initgraph(&gdriver,&gmode,NULL);
  88.     if (gdriver<EGA) {
  89.         closegraph();
  90.         printf("Error:  EGA or VGA required");
  91.         exit(1);
  92.     }
  93.     g_errorcode=graphresult();
  94.     if (g_errorcode!=grOk) {
  95.         printf("Graphics Error: %s\n", grapherrormsg(g_errorcode));
  96.         exit(1);
  97.     }
  98.     max_x=getmaxx();
  99.     max_y=getmaxy();
  100.     settextjustify(LEFT_TEXT,TOP_TEXT);
  101.     setbkcolor(EGA_BLUE);
  102. }/* initialize() */
  103.  
  104. void getopts (int argc, char **argv) {
  105.     int i;
  106.     char *cp;
  107.  
  108.     for (i=1;i<argc;i++) {
  109.         for (cp=argv[i];*cp;cp++) {
  110.             if (toupper(*cp)=='L') caps_only=0;
  111.             if (toupper(*cp)=='S') alnum_only=0;
  112.         }
  113.     }
  114. }/* getopts() */
  115.  
  116. void copyright_license () {
  117.     int p,q;
  118.  
  119.     setcolor(EGA_LIGHTRED);
  120.     settextstyle(SMALL_FONT,HORIZ_DIR,4);
  121.     p=(max_x-textwidth(copyright))/2;
  122.     outtextxy(p,0,copyright);
  123.     q=textheight(copyright);
  124.     p=(max_x-textwidth(address))/2;
  125.     outtextxy(p,q,address);
  126.     q+=textheight(address);
  127.     p=(max_x-textwidth(license))/2;
  128.     outtextxy(p,q,license);
  129. }/* copyright_license() */
  130.  
  131. void intro_song () {
  132.     double note;
  133.     int i,p,q;
  134.     char alpha[2]={0,0};
  135.  
  136.     setcolor(EGA_LIGHTCYAN);
  137.     delay(1);
  138.     settextstyle(SANS_SERIF_FONT,HORIZ_DIR,5);
  139.     p=(max_x-textwidth(lines[0]))/2;
  140.     q=textheight(lines[0])*3;
  141.     moveto(p,q/3);
  142.     outtext(lines[0]);
  143.     settextstyle(SANS_SERIF_FONT,HORIZ_DIR,7);
  144.     p=(max_x-textwidth(lines[1]))/2;
  145.     moveto(p,q);
  146.     q=q+textheight(lines[1])*3/2;
  147.     p=(max_x-textwidth(lines[2]))/2;
  148.     for (i=0;i<26;i++) {
  149.         if (i==13) moveto(p,q);
  150.         note=freq[song[i]];
  151.         sound((int)(note*2));
  152.         delay(12);
  153.         sound((int)note);
  154.         alpha[0]='A'+i;
  155.         setcolor(EGA_LIGHTBLUE+random(7));
  156.         outtext(alpha);
  157.         delay(len[i]*LEN-20);
  158.         nosound();
  159.         delay(20);
  160.         if (i==24) {  /* Puts in a beat for "and" */
  161.             sound((int)(note*2));
  162.             delay(12);
  163.             sound((int)note);
  164.             delay(len[i]*LEN-20);
  165.             nosound();
  166.             delay(20);
  167.         }
  168.     }
  169.     q=q+textheight(lines[2])*2;
  170.     setcolor(EGA_LIGHTMAGENTA);
  171.     settextstyle(SANS_SERIF_FONT,HORIZ_DIR,4);
  172.     p=(max_x-textwidth(lines[3]))/2;
  173.     moveto(p,q);
  174.     q=q+textheight(lines[3])*3/2;
  175.     p=(max_x-textwidth(lines[4]))/2;
  176.     for (i=0;i<16;i++) {
  177.         if (i==7) moveto(p,q);
  178.         note=freq[song[i]];
  179.         if(i!=12 && i!=14) {
  180.             sound((int)(note*2.));
  181.             delay(12);
  182.         }
  183.         sound((int)note);
  184.         outtext(end_song[i]);
  185.         delay(len[i]*LEN-20);
  186.         if (i!=11 && i!=13) nosound();
  187.         delay(20);
  188.     }
  189.     outtext("?");
  190.     delay(800);
  191. }/* intro_song() */
  192.  
  193. void put_exit_msg () {
  194.     static char *exit_msg[2]= {
  195.         "Press a key to show a letter and play a note.",
  196.         "Press Control-Z to exit."
  197.     };
  198.     int p,q,r;
  199.  
  200.     while(kbhit())getch(); /* clear keyboard buffer */
  201.     settextstyle(SMALL_FONT,HORIZ_DIR,5);
  202.     r=q=textheight(exit_msg[0])
  203.         +textheight(exit_msg[1]);
  204.     setviewport(0,0,max_x,r*3/2,0);
  205.     setcolor(EGA_WHITE);
  206.     p=(max_x-textwidth(exit_msg[0]))/2;
  207.     outtextxy(p,0,exit_msg[0]);
  208.     p=(max_x-textwidth(exit_msg[1]))/2;
  209.     q=textheight(exit_msg[0]);
  210.     outtextxy(p,q,exit_msg[1]);
  211.     setcolor(EGA_RED);
  212.     line(0,r*5/4,max_x,r*5/4);
  213.     setviewport(0,r*3/2,max_x,max_y,1);
  214. }/* put_exit_msg() */
  215.  
  216. void do_letters () {
  217.     int c,i,j,k,p,q,r,s;
  218.     unsigned int byte_size;
  219.     char far *image;
  220.     char in[]={0,0};
  221.     double note;
  222.     struct viewporttype viewset,port1,port2;
  223.     char buffer[64];
  224.  
  225.     buffer[0]=0;
  226.     setcolor(EGA_YELLOW);
  227.     getviewsettings((struct viewporttype far *)&viewset);
  228.     j=(viewset.bottom-viewset.top)*2/5;
  229.     port1.left=port2.left=0;
  230.     port1.right=port2.right=max_x;
  231.     port1.clip=port2.clip=1;
  232.     port1.top=viewset.top;
  233.     port1.bottom=viewset.top+(viewset.bottom-viewset.top)/2;
  234.     port2.top=port1.bottom+1;
  235.     port2.bottom=viewset.bottom;
  236.     clearviewport();
  237.     for (i=40;;i--) {
  238.         settextstyle(SANS_SERIF_FONT,HORIZ_DIR,i);
  239.         if(textheight((char far *)"X")<j) break;
  240.     } /* this sets the text height through run-time test */
  241.     setport(port1);
  242.     while ((c=getch()) != 26) {
  243.         if (c==0) { /* extended keystrokes are ignored */
  244.             c=getch();
  245.             twirp();
  246.             continue;
  247.         }
  248.         if (caps_only) in[0]=toupper(c);
  249.         else in[0]=c;
  250.         if ((alnum_only && !(isalnum(c)))
  251.             && !(isspace(c))) {
  252.             twirp();
  253.             continue;
  254.         }
  255.         if (isalpha(c)) {
  256.             i=toupper(c)-'A';
  257.             j=song[i];
  258.         } else if (isdigit(c)) j=scale[c=='0'?16:c-'0'+6];
  259.         else j=scale[random(8)];
  260.         note=freq[j];
  261.         sound((int)(note*4.));
  262.         delay(12);
  263.         sound((int)(note*2.));
  264.         delay(25);
  265.         sound((int)note);
  266.         s=0;
  267.         strcat(buffer,in);
  268.         if ((isspace(c)&&(c!=' '))||(textwidth(buffer)>=max_x)) {
  269.             getviewsettings(&viewset);
  270.             if (viewset.top==port1.top) {
  271.                 setport(port2);
  272.                 strcpy(buffer, in);
  273.             }
  274.             else {
  275.                 s=1;
  276.                 setport(port1);
  277.                 clearviewport();
  278.                 for(r=0;buffer[r];r++) ; /* loop through to end */
  279.                 buffer[r-1]=0;
  280.                 outtext(buffer);
  281.                 setport(port2);
  282.                 strcpy(buffer,in);
  283.             }
  284.         }
  285.         if (s&&(isspace(c))) {
  286.             buffer[0]=0;
  287.             clearviewport();
  288.         } else {
  289.             clearviewport();
  290.             outtext(buffer);
  291.         }
  292.         if (!kbhit()) delay(300);
  293.         nosound();
  294.     }
  295. }/* do_letters() */
  296.  
  297. void twirp () {
  298.     sound((int)MIDDLE_C*8);
  299.     delay(6);
  300.     sound((int)MIDDLE_C*4);
  301.     delay(12);
  302.     sound((int)MIDDLE_C*2);
  303.     delay(50);
  304.     sound((int)MIDDLE_C*4);
  305.     delay(3);
  306.     sound((int)MIDDLE_C*2);
  307.     delay(50);
  308.     sound((int)MIDDLE_C*4);
  309.     delay(3);
  310.     nosound();
  311. }/* twirp() */
  312.